import 'package:flutter/material.dart';
import 'package:flutter_common/flutter_common.dart';
import 'click.dart';

class FxSearchContainer extends StatelessWidget {
  final FxSearchTextField body;
  final Widget? action;
  final double? height;

  const FxSearchContainer({Key? key, required this.body, this.action, this.height})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: Colors.white,
      height: height,
      padding: action == null
          ? const EdgeInsets.symmetric(vertical: 10, horizontal: 15)
          : const EdgeInsets.only(left: 15, right: 0, top: 10, bottom: 10),
      child: Row(
        children: [Expanded(child: body), if (action != null) action!],
      ),
    );
  }
}

class FxSearchTextField extends StatefulWidget {
  /// 提示
  final String hint;

  final String text;

  ///文本控制器
  final TextEditingController? controller;

  ///焦点
  final FocusNode? focusNode;

  ///搜索编辑框
  final ValueChanged<String>? onSearch;

  /// 是否允许编辑
  final bool isEditable;

  /// 搜索栏点击时间，仅仅在不可编辑的时候才可以使用
  final VoidCallback? onClick;

  final TextStyle? hintStyle;
  final TextStyle? style;
  //final String? heroTag;

  final VoidCallback? onClearClick;
  final Color backgroundColor;
  final BoxDecoration? decoration;
  final EdgeInsetsGeometry padding;

  const FxSearchTextField(
      {Key? key,
        this.hint = "搜索",
        this.text = '',
        this.controller,
        this.focusNode,
        this.onSearch,
        this.isEditable = true,
        this.onClearClick,
        this.onClick,
        this.hintStyle,
        this.backgroundColor = const Color(0xFFF9F9F9),
        this.style,
        this.decoration,
        this.padding = const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
      })
      : super(key: key);

  @override
  State<FxSearchTextField> createState() => _FxSearchTextFieldState();
}

class _FxSearchTextFieldState extends State<FxSearchTextField> with FxState {
  late TextEditingController _controller;
  bool _isShowClear = false;

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? TextEditingController(text: widget.text);
    _isShowClear = _controller.text.isNotEmpty;
    _controller.addListener(onControllerValueChanged);
  }

  @override
  Widget build(BuildContext context) {
    const borderRadius = BorderRadius.all(Radius.circular(25));
    var decoration = widget.decoration ?? BoxDecoration(
        color: widget.backgroundColor,
        borderRadius: borderRadius);
    final isEnableClick = !widget.isEditable && widget.onClick != null;
    Widget child = Container(
      decoration: isEnableClick ? null : decoration,
      padding: widget.padding,
      child: Row(
        children: [
          Image.asset("assets/ic_search.png", width: 16, height: 16, package: 'flutter_common',),
          const SizedBox(width: 10),
          Expanded(
            child: contentWidget(),
          ),
          if (isShowClear)
            FxClick(
              onTap: onClearClick,
              child: Container(
                margin: const EdgeInsets.only(left: 10),
                child:
                Image.asset("assets/ic_clear.png", width: 16, height: 16, package: 'flutter_common',),
              ),
            )
        ],
      ),
    );
    if (isEnableClick) {
      child = FxClick(
        onTap: widget.onClick,
        decoration: decoration,
        borderRadius: borderRadius,
        child: child,
      );
    }
    return child;
  }

  Widget contentWidget() {
    if (widget.isEditable) {
      return TextField(
        focusNode: widget.focusNode,
        controller: _controller,
        maxLines: 1,
        decoration: InputDecoration(
            isCollapsed: true,
            hintStyle: widget.hintStyle ?? const TextStyle(fontSize: 14),
            hintText: widget.hint,
            border: InputBorder.none),
        onSubmitted: widget.onSearch,
        textInputAction: TextInputAction.search,
        style: widget.style ?? const TextStyle(fontSize: 14),
      );
    } else {
      final text = _controller.text;
      if (text.isEmpty) {
        return Text(widget.hint, );
      } else {
        return Text(text, );
      }
    }
  }

  void onControllerValueChanged() {
    bool current = _controller.text.isNotEmpty;
    if (current != _isShowClear) {
      mountedState(() {
        _isShowClear = current;
      });
    }
  }

  void onClearClick() {
    _controller.text = "";
    widget.onClearClick?.call();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.removeListener(onControllerValueChanged);
  }

  bool get isShowClear => _isShowClear;
}
